package cn.wolfcode.shop.cloud.web.controller;

import cn.wolfcode.shop.cloud.common.BusinessException;
import cn.wolfcode.shop.cloud.common.Result;
import cn.wolfcode.shop.cloud.redis.RedisService;
import cn.wolfcode.shop.cloud.redis.SeckillKeyPrefix;
import cn.wolfcode.shop.cloud.util.SeckillGoodsCodeMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
public class PathController {
    @Autowired
    private RedisService redisService;

    @RequestMapping("/getPath")
    public Result<String> getPath(String uuid,String verifyCode){
        String path = UUID.randomUUID().toString();
        redisService.set(SeckillKeyPrefix.SECKILL_PATH,uuid,path);

        if (StringUtils.isEmpty(verifyCode)){
            throw new BusinessException(SeckillGoodsCodeMsg.VERIFY_CODE_EMPTY);
        }
        String code = redisService.get(SeckillKeyPrefix.SECKILL_VERIFY_CODE, uuid, String.class);
        if (!verifyCode.equals(code)){
            throw new BusinessException(SeckillGoodsCodeMsg.VERIFY_CODE_ERROR);
        }
        return Result.success(path);
    }
}
